package com.bupt.hotel.hvac.controller;

import com.bupt.hotel.hvac.model.entity.RoomState;
import com.bupt.hotel.hvac.service.ACService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 房间服务控制器 - 统一接口标准
 * 提供房间信息查询功能
 */
@Slf4j
@RestController
@RequestMapping("/api/rooms")
public class RoomsController {

    private final ACService acService;

    public RoomsController(ACService acService) {
        this.acService = acService;
    }

    /**
     * 获取所有房间
     * GET /api/rooms
     */
    @GetMapping
    public List<Map<String, Object>> getAllRooms() {
        List<RoomState> rooms = acService.getAllRoomStates();
        
        return rooms.stream()
                .map(this::convertRoomStateToMap)
                .collect(Collectors.toList());
    }

    /**
     * 获取房间的详细信息
     * GET /api/rooms/{roomId}
     */
    @GetMapping("/{roomId}")
    public Map<String, Object> getRoomDetails(@PathVariable String roomId) {
        log.info("查询房间详细信息 - 房间: {}", roomId);
        
        try {
            RoomState room = acService.getRoomState(roomId);
            
            if (room == null) {
                Map<String, Object> result = new HashMap<>();
                result.put("error", "房间不存在");
                return result;
            }
            
            return convertRoomStateToMap(room);
        } catch (Exception e) {
            log.error("查询房间详细信息失败: {}", e.getMessage());
            Map<String, Object> result = new HashMap<>();
            result.put("error", e.getMessage());
            return result;
        }
    }

    /**
     * 转换RoomState到Map
     */
    private Map<String, Object> convertRoomStateToMap(RoomState room) {
        Map<String, Object> result = new HashMap<>();
        result.put("roomId", room.getRoomId());
        result.put("isOn", room.getIsOn());
        result.put("mode", room.getMode() != null ? room.getMode().name() : null);
        result.put("currentTemp", room.getCurrentTemp());
        result.put("initialTemp", room.getInitialTemp());
        result.put("targetTemp", room.getTargetTemp());
        result.put("fanSpeed", room.getFanSpeed() != null ? room.getFanSpeed().name() : null);
        result.put("status", room.getStatus() != null ? room.getStatus().name() : null);
        result.put("totalCost", room.getTotalCost());
        result.put("totalPowerConsumption", room.getTotalPowerConsumption());
        result.put("lastUpdateTime", room.getLastUpdateTime() != null 
                ? room.getLastUpdateTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() 
                : null);
        result.put("serviceStartTime", room.getServiceStartTime() != null 
                ? room.getServiceStartTime().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() 
                : null);
        
        return result;
    }
}
